package com.sgb.kjwl.view.ui.activity.self.packers;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.request.RequestOptions;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.decoration.GridSpacingItemDecoration;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.tools.ScreenUtils;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivitySelfTeamWorkerEditBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.self.SelfTeamIntroduceEntity;
import com.sgb.kjwl.model.entity.self.SelfTeamWorkerAddEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.view.ui.activity.PhotoViewActivity;
import com.sgb.kjwl.view.ui.adapter.self.packers.SelfLabelAdapter;
import com.sgb.link.view.adapter.SelfTeamPicAdapter;
import com.swgk.core.view.CityPickBuilder;
import com.swgk.core.view.CityPickView;
import com.sgb.kjwl.view.widget.SelfTeamWorkerProjectView;
import com.sgb.kjwl.viewmodel.self.packers.SelfTeamWorkerEditModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.customview.GlideRoundTransform;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.CheckUtils;
import com.swgk.core.util.MToast;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

/**
 * description:个人中心-团队工人发布(找工人编辑发布)
 * @author wangyejun
 * @date 2020/9/4
 */
public class SelfTeamWorkerEditActivity extends AppActivity implements ItemInputTextView.OnWholeItemClickListener {
    @Inject
    SelfTeamWorkerEditModel viewModel;
    private ActivitySelfTeamWorkerEditBinding binding;

    private SelfTeamWorkerAddEntity mEntity;

    private SelfTeamPicAdapter mPicAdapter;
    private List<String> mUploadImgList;
    /**
     * 防止重复上传
     */
    private Map<String, String> mCacheImgMap;
    private int mCurPicNum;
    public static final int REQUEST_AVATAR_CODE = 10105;
    private String avatar = "";
    private RequestManager requestManager;

    private SelfLabelAdapter labelAdapter;

    private List<PickSelectEntity> pickProjectCategory;
    private List<PickSelectEntity> pickWorkLevel;
    private List<PickSelectEntity> pickWorkType;
    private List<PickSelectEntity> pickWorkerSex;

    /**
     * 一级联动数据
     */
    private List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> addressEntityList;
    /**
     * 二级联动数据
     */
    private final List<List<AddressEntity<List<AddressEntity>>>> opt2 = new ArrayList<>();
    /**三级联动数据
     *
     */
    private final List<List<List<AddressEntity>>> opt3 = new ArrayList<>();

    private Thread thread;
    private static final int MSG_LOAD_DATA = 0x0001;
    private static final int MSG_LOAD_SUCCESS = 0x0002;
    private boolean isLoaded = false;

    private int cityPosition1;
    private int cityPosition2;
    private int cityPosition3;

    public static void start(Context context, int requestCode) {
        Intent intent = new Intent();
        intent.setClass(context, SelfTeamWorkerEditActivity.class);
        ((SelfTeamWorkerActivity) context).startActivityForResult(intent, requestCode);
    }
    public static void start(Context context,String no) {
        Intent intent = new Intent();
        intent.setClass(context, SelfTeamWorkerEditActivity.class);
        intent.putExtra("no", no);
        context.startActivity(intent);
    }
    public static void start(Context context, String no, int requestCode) {
        Intent intent = new Intent();
        intent.setClass(context, SelfTeamWorkerEditActivity.class);
        intent.putExtra("no", no);
        ((SelfTeamWorkerActivity) context).startActivityForResult(intent, requestCode);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder().appComponent(appComponent).selfModule(new SelfModule(this)).build().inject(this);
    }

    @Override
    protected boolean statusBarDark() {
        return false;
    }

    @Override
    protected void initView() {
        requestManager = Glide.with(this);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_self_team_worker_edit);
        binding.tvAddProject.setOnClickListener(this);
        binding.ivWorkerAvatar.setOnClickListener(this);
        binding.btnRelease.setOnClickListener(this);
        binding.etWorkType.setOnWholeItemClickListener(this);
        binding.etAddress.setOnWholeItemClickListener(this);
        binding.etHometown.setOnWholeItemClickListener(this);
        binding.etWorkerSex.setOnWholeItemClickListener(this);
        binding.etSpecialtyType.setOnWholeItemClickListener(this);
        binding.etProficiency.setOnWholeItemClickListener(this);
        binding.etWorkerNation.setOnWholeItemClickListener(this);
        binding.etWorkAddress.setOnWholeItemClickListener(this);
        binding.etAge.setInputType(InputType.TYPE_CLASS_NUMBER);
        binding.etPhone.setInputType(InputType.TYPE_CLASS_PHONE);
        binding.etWorkAge.setInputType(InputType.TYPE_CLASS_NUMBER);

        initPic();
    }

    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (TextUtils.isEmpty(getIntent().getStringExtra("no"))) {
            mActionBar.setTitle("发布工人");
            mEntity = new SelfTeamWorkerAddEntity();
            binding.setViewModel(mEntity);
        } else {
            mActionBar.setTitle("编辑工人");
            viewModel.getAddressModel(0);
        }
    }

    @Override
    protected void init() {
        viewModel.getTeamInfoLabel();
    }

    private void initPic() {
        mCacheImgMap = new HashMap<>();
        mUploadImgList = new ArrayList<>();
        GridLayoutManager manager = new GridLayoutManager(this, 3);
        binding.rvPic.setLayoutManager(manager);
        mPicAdapter = new SelfTeamPicAdapter(this, new ArrayList<>(), AppConstant.UPLOAD_MAX_SIZE, manager);
        binding.rvPic.setHasFixedSize(false);
        binding.rvPic.addItemDecoration(new GridSpacingItemDecoration(4, ScreenUtils.dip2px(this, 2), false));
        binding.rvPic.setAdapter(mPicAdapter);
        mPicAdapter.setOnItemClick(new AdapterOnItemClick<String>() {
            @Override
            public void onItemClick(String s, int position) {
                if (TextUtils.isEmpty(s)) {
                    viewModel.pictureSelector(mPicAdapter.hasCount(), PictureConfig.CHOOSE_REQUEST);
                } else {
                    Intent intent = new Intent(getActivity(), PhotoViewActivity.class);
                    intent.putExtra(PhotoViewActivity.KEY_PHOTO_URLS, (Serializable) mPicAdapter.getDatas());
                    intent.putExtra(PhotoViewActivity.KEY_PHOTO_POSITION, position);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    public void OnWholeItemClick(View v) {
        int id = v.getId();
        if (id == R.id.et_worker_sex) {
            showWorkerSexPop();
        } else if (id == R.id.et_work_type) {
            if (null == pickWorkType) {
                viewModel.getWorkType();
            } else {
                showPickWorkTypePop(pickWorkType);
            }
        } else if (id == R.id.et_specialty_type) {
            if (null == pickProjectCategory) {
                viewModel.getProTypeList();
            } else {
                showProjectCategoryPop(pickProjectCategory);
            }
        } else if (id == R.id.et_proficiency) {
            if (null == pickWorkLevel) {
                viewModel.getWorkLevel();
            } else {
                showPickWorkLevelPop(pickWorkLevel);
            }
        } else if (id == R.id.et_hometown) {
            viewModel.showAddressPop();
        } else if (id == R.id.et_address) {
            if (addressEntityList != null) {
                showCityPop();
            } else {
                viewModel.getAddressModel(1);
            }
        } else if (id == R.id.et_worker_nation) {
            viewModel.getNationList();
        } else if (id == R.id.et_work_address) {
            SelfServiceAreaActivity.start(this, mEntity.getWorkArea(), AppConstant.REQ_SERVICE, "希望工作地");
        }
    }

    @Override
    public void onMultiClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            onBackPressed();
        } else if (id == R.id.tv_add_project) {
            if (addressEntityList != null) {
                SelfTeamWorkerProjectView view = new SelfTeamWorkerProjectView(this);
                view.setBean(null, binding.projectLayout.getChildCount() + 1, addressEntityList);
                binding.projectLayout.addView(view);
                if (binding.projectLayout.getChildCount() == 3) {
                    binding.tvAddProject.setVisibility(View.GONE);
                }
                binding.nestedScrollView.scrollBy(0, binding.projectLayout.getHeight());
            } else {
                viewModel.getAddressModel(2);
            }
        } else if (id == R.id.btn_release) {
            if (TextUtils.isEmpty(binding.etName.getContent())) {
                MToast.showToast(this, binding.etName.getContent_edt().getHint().toString());
                return;
            }
            if (TextUtils.isEmpty(binding.etWorkerSex.getContent())) {
                MToast.showToast(this, binding.etWorkerSex.getContent_edt().getHint().toString());
                return;
            }
            if (TextUtils.isEmpty(binding.etAge.getContent())) {
                MToast.showToast(this, binding.etAge.getContent_edt().getHint().toString());
                return;
            }
            if (TextUtils.isEmpty(binding.etHometown.getContent())) {
                MToast.showToast(this, binding.etHometown.getContent_edt().getHint().toString());
                return;
            }
            if (TextUtils.isEmpty(binding.etAddress.getContent())) {
                MToast.showToast(this, binding.etAddress.getContent_edt().getHint().toString());
                return;
            }
            if (TextUtils.isEmpty(binding.etPhone.getContent())) {
                MToast.showToast(this, binding.etPhone.getContent_edt().getHint().toString());
                return;
            }
            if (TextUtils.isEmpty(binding.etWorkAge.getContent())) {
                MToast.showToast(this, binding.etWorkAge.getContent_edt().getHint().toString());
                return;
            }
            if (TextUtils.isEmpty(binding.etWorkerNation.getContent())) {
                MToast.showToast(this, binding.etWorkerNation.getContent_edt().getHint().toString());
                return;
            }
            if (TextUtils.isEmpty(binding.etWorkType.getContent())) {
                MToast.showToast(this, binding.etWorkType.getContent_edt().getHint().toString());
                return;
            }
            if (TextUtils.isEmpty(binding.etWorkAddress.getContent())) {
                MToast.showToast(this, binding.etWorkAddress.getContent_edt().getHint().toString());
                return;
            }
            if (TextUtils.isEmpty(binding.etSpecialtyType.getContent())) {
                MToast.showToast(this, binding.etSpecialtyType.getContent_edt().getHint().toString());
                return;
            }
            if (TextUtils.isEmpty(binding.etProficiency.getContent())) {
                MToast.showToast(this, binding.etProficiency.getContent_edt().getHint().toString());
                return;
            }
            if (Integer.parseInt(binding.etAge.getContent()) < Integer.parseInt(binding.etWorkAge.getContent())) {
                MToast.showToast(this, "工龄不可以超过年龄");
                return;
            }
            if (!CheckUtils.isMobile(binding.etPhone.getContent().trim())) {
                Toast.makeText(this, "请填写正确的联系电话", Toast.LENGTH_SHORT).show();
                return;
            }
            String temp = binding.etPhone.getContent();
            if (binding.etPhone.getContent().startsWith("+86")) {
                temp = binding.etPhone.getContent().substring(3);
            }
            mEntity.setContactPhone(temp);
            mUploadImgList.clear();
            mCurPicNum = -1;
            uploadAvatar();
        } else if (id == R.id.iv_worker_avatar) {
            viewModel.pictureSelector(1, REQUEST_AVATAR_CODE);
        }
    }

    private void uploadAvatar() {
        //过滤已上传头像且上传新头像
        if (TextUtils.isEmpty(mEntity.getAvatarAddress()) || !TextUtils.isEmpty(avatar)) {
            if (null != avatar && !TextUtils.isEmpty(avatar)) {
                File file = new File(avatar);
                viewModel.uploadAvatarFileModel(file, 1);
            } else {
                uploadPic();
            }
        } else {
            uploadPic();
        }
    }

    private void uploadPic() {
        mCurPicNum++;
        int size = mPicAdapter.getRealCount();
        if (size > 0 && mCurPicNum < size) {
            String path = mPicAdapter.getDatas().get(mCurPicNum);
            if (path.startsWith("http")) {
                mUploadImgList.add(path); // 保存已上传图片
                uploadPic();
            } else {
                // 过滤已上传的
                if (!TextUtils.isEmpty(mCacheImgMap.get(path))) {
                    mUploadImgList.add(mCacheImgMap.get(path));
                    uploadPic();
                } else {
                    File file = new File(mPicAdapter.getDatas().get(mCurPicNum));
                    viewModel.uploadMultiFileModel(file, mCurPicNum + 1, size);
                }
            }
        } else {
            release();
        }
    }

    private void release() {
        int childCount = binding.projectLayout.getChildCount();
        List<SelfTeamWorkerAddEntity.WorkExpListBean> listBeans = new ArrayList<>();
        if (childCount > 0) {
            for (int i = 0; i < childCount; i++) {
                View childAt = binding.projectLayout.getChildAt(i);
                if (childAt instanceof SelfTeamWorkerProjectView) {
                    SelfTeamWorkerAddEntity.WorkExpListBean listBean = ((SelfTeamWorkerProjectView) childAt).getListBean();
                    listBeans.add(listBean);
                }
            }
        }
        mEntity.setWorkExpList(listBeans);
        mEntity.setFilePath(mUploadImgList);
        viewModel.addWorker(mEntity);
    }

    public void updateWorkerEntity(SelfTeamWorkerAddEntity data) {
        if (data != null) {
            mEntity = data;
            binding.setViewModel(data);
            if (TextUtils.isEmpty(data.getAvatarAddress())) {
                requestManager.load(R.mipmap.worker_avatar).into(binding.ivWorkerAvatar);
            } else {
                RequestOptions options = new RequestOptions();
                options.transform(new GlideRoundTransform(this, 5));
                requestManager.load(data.getAvatarAddress()).apply(options).into(binding.ivWorkerAvatar);
            }
            if (data.getSex() == 1){
                binding.etWorkerSex.setContent("男");
            }
            if (data.getSex() == 2){
                binding.etWorkerSex.setContent("女");
            }
            if (!TextUtils.isEmpty(data.getLegalProvName())){
                binding.etHometown.setContent(data.getLegalProvName() + data.getLegalCityName());
            }
            setCheckDefaultList(data.getLabel());
            if (mEntity.getFilePath() != null && mEntity.getFilePath().size() > 0) {
                mPicAdapter.setData(mEntity.getFilePath());
            }
            if (mEntity.getWorkExpList() != null && mEntity.getWorkExpList().size() > 0) {
                for (int i = 0; i < mEntity.getWorkExpList().size(); i++) {
                    SelfTeamWorkerProjectView view = new SelfTeamWorkerProjectView(this);
                    view.setBean(mEntity.getWorkExpList().get(i), i + 1, addressEntityList);
                    binding.projectLayout.addView(view);
                }
                if (binding.projectLayout.getChildCount() >= 3) {
                    binding.tvAddProject.setVisibility(View.GONE);
                }
            }
        }
    }

    /**
     * 发布修改数据成功
     */
    public void editEntitySuccess() {
        setResult(RESULT_OK, new Intent());
        finish();
    }

    public void showEmpty() {
        binding.emptyView.setVisibility(View.VISIBLE);
        DialogHelper.dismissProgressDialog();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == PictureConfig.CHOOSE_REQUEST) {
                List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                mPicAdapter.setPicData(selectList);
            }
            if (requestCode == REQUEST_AVATAR_CODE) {
                List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                RequestOptions options = new RequestOptions();
                options.transform(new GlideRoundTransform(this, 5));
                Glide.with(this).load(selectList.get(0).getRealPath()).apply(options).into(binding.ivWorkerAvatar);
                avatar = selectList.get(0).getRealPath();
            }
            if (requestCode == AppConstant.REQ_SERVICE) {
                if (data != null) {
                    List<SelfTeamIntroduceEntity.ServiceAreasBean> serviceAreasBeans = (List<SelfTeamIntroduceEntity.ServiceAreasBean>) data.getSerializableExtra(AppConstant.EXTRA_OBJ);
                    if (serviceAreasBeans.size() >= 3) {
                        mEntity.setWorkArea(serviceAreasBeans);
                        StringBuilder stringBuilder = new StringBuilder();
                        for (SelfTeamIntroduceEntity.ServiceAreasBean bean : serviceAreasBeans) {
                            stringBuilder.append(bean.getName()).append("、");
                        }
                        binding.etWorkAddress.setContent(stringBuilder.substring(0, stringBuilder.length() - 1));
                    }else {
                        MToast.showToast(this, "至少选择三个希望工作地点");
                    }
                }
            }
        }
    }

    /**
     * 上传头像回调
     * @param toString
     */
    public void uploadAvatarResult(String toString) {
        mEntity.setAvatarAddress(toString);
        avatar = null;
        uploadPic();
    }

    /**
     * 图片上传回调
     * @param fromString
     * @param toString
     */
    public void uploadPicInfo(String fromString, String toString) {
        mUploadImgList.add(toString);
        mCacheImgMap.put(fromString, toString);
        uploadPic();
    }

    /**
     * 籍贯选择回调
     * @param opt1
     * @param opt2
     */
    public void updateAddress(PickSelectEntity<List<PickSelectEntity>> opt1, PickSelectEntity opt2) {
        mEntity.setLegalProvName(opt1.getProjectscale());
        mEntity.setLegalCityName(opt2.getProjectscale());
        mEntity.setProvinceNo(opt1.getNo());
        mEntity.setLegalCityNo(opt2.getNo());
        binding.etHometown.setContent(opt1.getProjectscale() + opt2.getProjectscale());
    }

    public void showProjectCategoryPop(List<PickSelectEntity> list) {
        if (null == pickProjectCategory) {
            pickProjectCategory = new ArrayList<>();
            pickProjectCategory.addAll(list);
        }
        if (pickProjectCategory.size() > 0) {
            OptionsPickerView projectCategoryOption = new OptionsPickerBuilder(SelfTeamWorkerEditActivity.this, (options1, options2, options3, v) -> {
                binding.etSpecialtyType.setContent(pickProjectCategory.get(options1).getProjectscale());
                mEntity.setCategoryNo(pickProjectCategory.get(options1).getNo());
            }).setCancelColor(ContextCompat.getColor(this, R.color.color_666666)).setTitleColor(ContextCompat.getColor(this, R.color.color_969696)).build();
            projectCategoryOption.setPicker(pickProjectCategory);
            projectCategoryOption.setSelectOptions(0);
            projectCategoryOption.show();
        }
    }

    public void showWorkerSexPop() {
        if (null == pickWorkerSex) {
            pickWorkerSex = new ArrayList<>();
            PickSelectEntity entityMan = new PickSelectEntity();
            entityMan.setProjectscale("男");
            entityMan.setNo("1");
            PickSelectEntity entityWoMan = new PickSelectEntity();
            entityWoMan.setProjectscale("女");
            entityWoMan.setNo("2");
            pickWorkerSex.add(entityMan);
            pickWorkerSex.add(entityWoMan);
        }
        if (pickWorkerSex.size() > 0) {
            OptionsPickerView projectCategoryOption = new OptionsPickerBuilder(SelfTeamWorkerEditActivity.this, (options1, options2, options3, v) -> {
                binding.etWorkerSex.setContent(pickWorkerSex.get(options1).getProjectscale());
                mEntity.setSex(Integer.parseInt(pickWorkerSex.get(options1).getNo()));
            }).setCancelColor(ContextCompat.getColor(this, R.color.color_666666)).setTitleColor(ContextCompat.getColor(this, R.color.color_969696)).build();
            projectCategoryOption.setPicker(pickWorkerSex);
            projectCategoryOption.setSelectOptions(0);
            projectCategoryOption.show();
        }
    }

    public void showPickWorkTypePop(List<PickSelectEntity> list) {
        if (null == pickWorkType) {
            pickWorkType = new ArrayList<>();
            pickWorkType.addAll(list);
        }
        if (pickWorkType.size() > 0) {
            OptionsPickerView projectCategoryOption = new OptionsPickerBuilder(SelfTeamWorkerEditActivity.this, (options1, options2, options3, v) -> {
                binding.etWorkType.setContent(pickWorkType.get(options1).getProjectscale());
                mEntity.setWorkType(pickWorkType.get(options1).getNo());
            }).setCancelColor(ContextCompat.getColor(this, R.color.color_666666)).setTitleColor(ContextCompat.getColor(this, R.color.color_969696)).build();
            projectCategoryOption.setPicker(pickWorkType);
            projectCategoryOption.setSelectOptions(0);
            projectCategoryOption.show();
        }
    }

    public void showPickWorkLevelPop(List<PickSelectEntity> list) {
        if (null == pickWorkLevel) {
            pickWorkLevel = new ArrayList<>();
            pickWorkLevel.addAll(list);
        }
        if (pickWorkLevel.size() > 0) {
            OptionsPickerView projectCategoryOption = new OptionsPickerBuilder(SelfTeamWorkerEditActivity.this, (options1, options2, options3, v) -> {
                binding.etProficiency.setContent(pickWorkLevel.get(options1).getProjectscale());
                mEntity.setProficiency(pickWorkLevel.get(options1).getNo());
            }).setCancelColor(ContextCompat.getColor(this, R.color.color_666666)).setTitleColor(ContextCompat.getColor(this, R.color.color_969696)).build();
            projectCategoryOption.setPicker(pickWorkLevel);
            projectCategoryOption.setSelectOptions(0);
            projectCategoryOption.show();
        }
    }

    /**
     * 所在地区选择
     * @param data
     * @param flag
     */
    public void showAddressPop(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data, int flag) {
        if (flag == 2) {
            SelfTeamWorkerProjectView view = new SelfTeamWorkerProjectView(this);
            view.setBean(null, binding.projectLayout.getChildCount() + 1, data);
            binding.projectLayout.addView(view);
            if (binding.projectLayout.getChildCount() == 3) {
                binding.tvAddProject.setVisibility(View.GONE);
            }
            binding.nestedScrollView.scrollBy(0, binding.projectLayout.getHeight());
            return;
        }
        if (flag == 1) {
            this.addressEntityList = data;
            if (isLoaded) {
                showCityPop();
            } else {
                mHandler.sendEmptyMessage(MSG_LOAD_DATA);
            }
        }
        if (flag == 0){
            viewModel.detailWorker(getIntent().getStringExtra("no"));
        }
    }

    @SuppressWarnings("unchecked")
    public void showCityPop() {
        //条件选择器
        CityPickView cityOption = new CityPickBuilder(SelfTeamWorkerEditActivity.this, (options1, options2, options3, v) -> {
            String areaName;
            if (opt3.get(options1).get(options2).get(options3) == null) {
                areaName = "";
            } else {
                areaName = opt3.get(options1).get(options2).get(options3).getName();
                cityPosition3 = options3;
                mEntity.setAreaNo(opt3.get(options1).get(options2).get(options3).getNo());
                mEntity.setAreaName(areaName);
            }
            String content = addressEntityList.get(options1).getName()
                    + opt2.get(options1).get(options2).getName()
                    + areaName;
            binding.etAddress.setContent(content);
            cityPosition1 = options1;
            cityPosition2 = options2;
            mEntity.setProvinceName(addressEntityList.get(options1).getName());
            mEntity.setProvinceNo(addressEntityList.get(options1).getId());
            mEntity.setCityName(opt2.get(options1).get(options2).getName());
            mEntity.setCityNo(opt2.get(options1).get(options2).getNo());
        }).setCancelColor(ContextCompat.getColor(this, R.color.color_666666)).setTitleColor(ContextCompat.getColor(this, R.color.color_969696)).build();
        cityOption.setPicker(addressEntityList, opt2, opt3);
        cityOption.setSelectOptions(cityPosition1, cityPosition2, cityPosition3);
        cityOption.show();
    }

    private final Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == MSG_LOAD_DATA) {
                if (thread == null) {
                    //如果已创建就不再重新创建子线程了
                    thread = new Thread(() -> {
                        // 子线程中解析省市区数据
                        initJsonData();
                    });
                    thread.start();
                }
            } else if (msg.what == MSG_LOAD_SUCCESS) {
                showCityPop();
                isLoaded = true;
                thread = null;
            }
            return false;
        }
    });

    private void initJsonData() {
        //城市列表
        @SuppressWarnings("MismatchedQueryAndUpdateOfCollection") List<AddressEntity<List<AddressEntity>>> cityList;
        //区域列表
        List<List<AddressEntity>> areaList;
        for (int x = 0; x < addressEntityList.size(); x++) {
            cityList = new ArrayList<>();
            areaList = new ArrayList<>();
            if (mEntity != null && mEntity.getProvinceName() != null && mEntity.getProvinceName().equals(addressEntityList.get(x).getName())) {
                cityPosition1 = x;
            }
            opt2.add(addressEntityList.get(x).getChildren());
            for (int i = 0; i < addressEntityList.get(x).getChildren().size(); i++) {
                cityList.add(addressEntityList.get(x).getChildren().get(i));
                List<AddressEntity> cityAreaList = new ArrayList<>();
                //如果无地区数据，建议添加空字符串，防止数据为null 导致三个选项长度不匹配造成崩溃
                if (addressEntityList.get(x).getChildren().get(i).getChildren() == null
                        || addressEntityList.get(x).getChildren().get(i).getChildren().size() == 0) {
                    cityAreaList.add(null);
                } else {
                    cityAreaList.addAll(addressEntityList.get(x).getChildren().get(i).getChildren());
                }
                areaList.add(cityAreaList);
            }
            opt3.add(areaList);
        }
        if (mEntity != null) {
            if (mEntity.getCityName() != null) {
                for (int x2 = 0; x2 < opt2.size(); x2++) {
                    if (mEntity.getCityName().equals(addressEntityList.get(cityPosition1).getChildren().get(x2).getName())) {
                        cityPosition2 = x2;
                        break;
                    }
                }
            }
            if (mEntity.getAreaName() != null) {
                for (int x3 = 0; x3 < opt3.size(); x3++) {
                    if (mEntity.getAreaName().equals(addressEntityList.get(cityPosition1).getChildren().get(cityPosition2).getChildren().get(x3).getName())) {
                        cityPosition3 = x3;
                        break;
                    }
                }
            }
        }
        mHandler.sendEmptyMessage(MSG_LOAD_SUCCESS);
    }

    private  List<String> stringList;

    public void setLabelAdapterData(List<PickSelectEntity> labelList) {
        if (stringList == null) {
            stringList = new ArrayList<>();
        }
        if (labelAdapter == null) {
            labelAdapter = new SelfLabelAdapter(this, labelList);
        }
        binding.maxRec.setLayoutManager(new GridLayoutManager(this, 4));
        binding.maxRec.setAdapter(labelAdapter);
        labelAdapter.setMultiSelect(true);
        labelAdapter.setMost(3);
        labelAdapter.setOnItemMultiClick((pickSelectEntities, position) -> {
            stringList.clear();
            for (PickSelectEntity entity : pickSelectEntities) {
                stringList.add(entity.getNo());
            }
            mEntity.setLabel(stringList);
        });
    }

    public void setCheckDefaultList(List<String> checkDefaultList) {
        if (checkDefaultList != null && labelAdapter != null) {
            for (String s : checkDefaultList) {
                labelAdapter.setCheckDefault(Integer.parseInt(s) -1);
            }
        }
    }


    private List<PickSelectEntity> pickNation;

    public void showNationPop(List<PickSelectEntity> list) {
        if (null == pickNation) {
            pickNation = new ArrayList<>();
            pickNation.addAll(list);
        }
        if (pickNation.size() > 0) {
            OptionsPickerView projectCategoryOption = new OptionsPickerBuilder(SelfTeamWorkerEditActivity.this, (options1, options2, options3, v) -> {
                binding.etWorkerNation.setContent(pickNation.get(options1).getProjectscale());
                mEntity.setNation(pickNation.get(options1).getNo());
            }).setCancelColor(ContextCompat.getColor(this, R.color.color_666666)).setTitleColor(ContextCompat.getColor(this, R.color.color_969696)).build();
            projectCategoryOption.setPicker(pickNation);
            projectCategoryOption.setSelectOptions(0);
            projectCategoryOption.show();
        }
    }
}
